import request from '@/utils/request'
import store from '@/store'

// 接口方法, 只负责调用一个接口, 返回一个Promise对象

/**
 * 获取-所有频道列表
 * @returns Promise对象
 */
export const allChannelListAPI = () => {
  // return Promise对象到逻辑页面去接收请求响应的结果数据
  return request({
    url: '/v1_0/channels'
  })
}
/**
 * 注册登录
 * @param {*} param0 {mobile:手机号，code:验证码}
 * @returns Promise对象
 */
export const loginAPI = ({ mobile, code }) => {
  return request({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
      mobile,
      code
    }
  })
}
/**
 * 获取-用户选择的频道
 * @returns Promise对象
 */
export const getUsersChannelListAPI = () => {
  return request({
    url: '/v1_0/user/channels'
    // headers: {
    //   Authorization: `Bearer ${store.state.token}`
    // }
  })
}
/**
 * 获取-文章列表
 * @param {*} param0 { channel_id:频道id,timestamp:(默认传系统时间戳) 上一段文章中最后一个文章的时间戳, 用于获取下一页数据, }
 * @returns
 */
export const getArticleListAPI = ({
  channel_id
  , timestamp
}) => {
  return request({
    url: '/v1_0/articles',
    params: {
      channel_id,
      timestamp

    }
  })
}
/**
 * 文章-不喜欢
 * @param {*} param0
 * @returns
 */
export const articleDislikeAPI = (artId) => {
  return request({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    // headers: {
    //   Authorization: `Bearer ${store.state.token}`
    // },
    data: {
      target: artId
    }
  })
}
/**
 * 举报文章
 * @param {*} param0 { artId: 文章id, type: 举报类型数字, remark: 其他问题说明 }
 * @returns Promise对象
 */
export const reportArticleAPI = ({ artId, type, remark }) => {
  // 自己组织参数对象
  const dataObj = {
    target: artId,
    type
  }
  // 只有type类型值为0(其他问题), 才需要携带remark参数
  if (type === 0) {
    dataObj.remark = remark
  }

  return request({
    url: '/v1_0/article/reports',
    method: 'POST',
    // headers: {
    //   Authorization: `Bearer ${store.state.token}`
    // },
    data: dataObj
  })
}
/**
 * 设置-频道列表
 * @param {*} param0
 * @returns
 */
export const uploadUserChannelAPI = ({ channels }) => {
  return request({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels
    }
  })
}
/**
 * 获取-搜索联想建议
 * @param {*} param0
 * @returns
 */
export const getSuggestListAPI = ({ keyWords }) => {
  return request({
    url: '/v1_0/suggestion',
    method: 'GET',
    params: {
      q: keyWords
    }
  })
}
/**
 * 获取-搜索结果
 * @param {*} param0
 * @returns
 */
export const getSearchResultListAPI = ({ q, page, per_page }) => {
  return request({
    url: '/v1_0/search',
    method: 'GET',
    params: {
      q,
      page,
      per_page
    }
  })
}
// 获取-文章详情
export const getArticleDetailAPI = ({ artId }) => request({
  url: `/v1_0/articles/${artId}`
})
// 关注作者
export const followUserAPI = ({ target }) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target
  }
})
// 取关作者
export const unfollowUserAPI = ({ target }) => request({
  url: `/v1_0/user/followings/${target}`,
  method: 'DELETE'
})

// 文章 - 点赞
export const articleLikeAPI = ({ target }) => {
  return request({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target: target
    }
  })
}

// 文章 - 取消点赞
export const articleDisLikeAPI = ({ artId }) => {
  return request({
    url: `/v1_0/article/likings/${artId}`,
    method: 'DELETE'
  })
}
/**
 * 获取-文章评论列表
 * @param {*} param0
 * @returns
 */
export const getArticleCommentListAPI = ({
  source,
  offset = null,
  limit = 10
}) => {
  return request({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source,
      offset,
      limit
    }
  })
}

// 评论 - 喜欢
export const commentLikingAPI = ({ comId }) => {
  return request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: comId
    }
  })
}
// 评论-取消喜欢
export const commentDisLikingAPI = ({ comId }) => {
  return request({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'DELETE'
  })
}

export const sendCommetAPI = ({ artId, content }) => {
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: {
      target: artId,
      content: content
    }
  })
}

// 用户 - 基本资料
export const userInfoAPI = () => {
  return request({
    url: '/v1_0/user'
  })
}

// 用户- 个人资料(就为了获取生日)
export const userProfileAPI = () => {
  return request({
    url: '/v1_0/user/profile'
  })
}
// 用户- 更新头像
// 注意: formObj的值必须是一个表单对象
// '{"a": 10, "b": 20}' // 对象格式的JSON字符串
// new FormData() // 表单对象
export const updatePhotoAPI = (formObj) => {
  return request({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: formObj
    // 如果你的请求体内容是表单对象, 浏览器会自动携带请求头Content-Type为multipart/form-data
  })
}
export const patchUserProfileAPI = ({ name = '', gender = 0, birthday = '', real_name = '', intro = '' }) => {
  // 只有params参数，axios内部会判断值为null时，不会携带此参数名和值给后台
  // 我改某个字段的值，其他字段不能传递，如果传了就改了个空的生日
  // 解决：
  const dataObj = {
    name,
    gender,
    birthday,
    real_name,
    intro
  }
  const newObj = {}
  // 遍历dataobj.里每对key+value,有值才放到newObj
  for (const key in dataObj) {
    if (dataObj[key]) {
      newObj[key] = dataObj[key]
    }
  }

  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: newObj
  })
}
// 用户 - 更新token
export const refreshTokenAPI = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    // 请求拦截器统一携带的是token,而这次请求需要带的是refresh_token
    // 所以在axios请求拦截器里判断，就是为了这种情况准备的
    Authorization: `Bearer ${store.state.refresh_token}`
  }
})
